Conversation
fix: Support custom tag ordering
…port/v2--components--Microsoft.OpenApi chore(support/v2): release 2.4.3
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
|
| new HashSet<OpenApiTag>(value, OpenApiTagComparer.Instance); | ||
| _tags = value switch | ||
| { | ||
| HashSet<OpenApiTag> tags when tags.Comparer != EqualityComparer<OpenApiTag>.Default => value, |
Check warning
Code scanning / CodeQL
Reference equality test on System.Object Warning
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 22 days ago
In general, to fix this issue you should avoid using ==/!= between variables of type object or interface when the intent is value/semantic equality. Either cast to a more specific type that has an appropriate Equals/== implementation or call Equals explicitly. If the intent is to check identity, then use ReferenceEquals(a, b).
Here, the intent is: “if the existing HashSet’s comparer is not the default comparer, keep the existing set; otherwise, wrap the incoming set in a new HashSet with OpenApiTagComparer.Instance.” This logic should not depend on two comparer instances being the same object. The minimal behavioural change is to replace the reference comparison with a semantic equality comparison using Equals, handling null-safety. We can do this by using !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer) instead of tags.Comparer != EqualityComparer<OpenApiTag>.Default. This preserves the branch condition’s meaning (“comparer is different from the default”) but uses proper equality semantics instead of object identity. All other parts of the snippet remain unchanged, and no new imports or types are required.
Concretely, in src/Microsoft.OpenApi/Models/OpenApiDocument.cs, in the Tags property setter’s switch expression, update the HashSet<OpenApiTag> pattern clause to use !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer).
| @@ -93,7 +93,7 @@ | ||
| } | ||
| _tags = value switch | ||
| { | ||
| HashSet<OpenApiTag> tags when tags.Comparer != EqualityComparer<OpenApiTag>.Default => value, | ||
| HashSet<OpenApiTag> tags when !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer) => value, | ||
| SortedSet<OpenApiTag> => value, | ||
| #if NET | ||
| ImmutableSortedSet<OpenApiTag> => value, |



fixes #2678 port of #2679 to v3